package com.yand.controller;



import com.yand.api.entity.auth.VerifyCode;
import com.yand.api.util.SimpleCharVerifyCodeUtil;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 初始化controller 跳转登录页面
 */
@Api(tags = "初始化控制器")
@Controller
public class InitController {


    @RequestMapping("/welcome")
    public String toLoginPage(){
        return "login";
    }


    /**
     * 临时主页
     *
     * @return
     */
    @GetMapping("/temp")
    public ModelAndView toTempPage(){

        return new ModelAndView("temp");
    }

    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) {

        try {
            //设置长宽
            VerifyCode verifyCode = SimpleCharVerifyCodeUtil.generate(80, 28);

            String code = verifyCode.getCode();
            //将VerifyCode绑定session
            request.getSession().setAttribute("authcode", code);
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");

            response.getOutputStream().write(verifyCode.getImgBytes());

            response.getOutputStream().flush();

        } catch (IOException e) {

        }
    }



}
